package com.frxuexi.park2302.park

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.park2302.R
import com.frxuexi.park2302.databinding.ActivityParkEditBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/ParkEditActivity")
@AndroidEntryPoint
class ParkEditActivity : BaseActivity<ActivityParkEditBinding, ParkIntent, ParkViewModel>() {
    override val binding: ActivityParkEditBinding by lazy {
        ActivityParkEditBinding.inflate(
            layoutInflater
        )
    }
    override val vm: ParkViewModel by viewModels<ParkViewModel>()
    var areaId = 1
    var areaName = ""
    var type = ""
    var status = ""

    @Autowired
    lateinit var pid: String
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when (it.type) {
                            RequestType.UPDATE -> {
                                ToastUtils.showLong("修改成功")
                                finish()
                            }

                            else -> {
                                val item = it.data as ParkBeanItem
                                binding.parkaddCarNo.setText(item.spaceNumber)
                                binding.parkaddUser.setText(item.userName)
                                binding.parkaddCarNum.setText(item.licensePlate)
                                binding.parkaddRemark.setText(item.remarks)
                                areaId = item.areaId
                                areaName = item.areaName
                                type = item.spaceType
                                status = item.spaceStatus

                                when (item.spaceStatus) {
                                    "空闲" -> binding.parkaddRg.check(R.id.rb_kx)
                                    "已占用" -> binding.parkaddRg.check(R.id.rb_zy)
                                    "维护中" -> binding.parkaddRg.check(R.id.rb_wh)
                                }

                                val areaList =
                                    mutableListOf<String>("A区停车场", "B区停车场", "C区停车场")
                                if (!areaList.contains(item.areaName)) {
                                    areaList.add(0, item.areaName)
                                }
                                val areaAdapter = ArrayAdapter(
                                    this@ParkEditActivity,
                                    android.R.layout.simple_spinner_item,
                                    areaList
                                )
                                areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                                binding.parkaddArea.adapter = areaAdapter
                                binding.parkaddArea.onItemSelectedListener =
                                    object : AdapterView.OnItemSelectedListener {
                                        override fun onItemSelected(
                                            parent: AdapterView<*>?,
                                            view: View?,
                                            position: Int,
                                            id: Long
                                        ) {
                                            areaId = position + 1
                                            areaName = areaList[position]
                                        }

                                        override fun onNothingSelected(parent: AdapterView<*>?) {
                                        }

                                    }


                                val typeList = mutableListOf<String>("标准车位", "VIP车位")
                                if (!typeList.contains(item.spaceType)) {
                                    typeList.add(0, item.spaceType)
                                }
                                val typeAdapter = ArrayAdapter(
                                    this@ParkEditActivity,
                                    android.R.layout.simple_spinner_item,
                                    typeList
                                )
                                typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                                binding.parkaddType.adapter = typeAdapter
                                binding.parkaddType.onItemSelectedListener =
                                    object : AdapterView.OnItemSelectedListener {
                                        override fun onItemSelected(
                                            parent: AdapterView<*>?,
                                            view: View?,
                                            position: Int,
                                            id: Long
                                        ) {
                                            type = typeList[position]
                                        }

                                        override fun onNothingSelected(parent: AdapterView<*>?) {
                                        }

                                    }
                            }
                        }

                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        vm.sendIntent(ParkIntent.getParkById(pid.toInt()))
        binding.headParkAdd.apply {
            headBack.setOnClickListener {
                finish()
            }
            headTitle.setText("车位修改")
            headAdd.setImageResource(R.drawable.ic_action_ok)
            headAdd.setOnClickListener {
                val carNum = binding.parkaddCarNo.text.toString()
                val remark = binding.parkaddRemark.text.toString()
                val carPlate = binding.parkaddCarNum.text.toString()
                val uname = binding.parkaddUser.text.toString()
                val item =
                    ParkBeanItem(
                        areaId,
                        areaName,
                        pid.toInt(),
                        carPlate,
                        remark,
                        carNum,
                        status,
                        type,
                        0,
                        uname,0
                    )
                vm.sendIntent(ParkIntent.updatePark(pid.toInt(), item))
            }
        }

        binding.parkaddRg.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.rb_kx->status="空闲"
                R.id.rb_zy->status="已占用"
                R.id.rb_wh->status="维护中"
            }
        }


    }

}